//
//  BIDCommonRepairListViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/1.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDCommonRepairListViewController.h"
#import "BIDCommonRepairCell.h"
#import "BIDAddCommonRepairViewController.h"
#import "BIDCommonRepairDetailViewController.h"

//公共报修
static NSString *commonRepairListURL = @"client/repair/publicRepair.action";

@interface BIDCommonRepairListViewController ()<BIDBaseListViewDelegate>
{
    BOOL _bRegister;
}
@end

@implementation BIDCommonRepairListViewController
@synthesize userID;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"公共报修";    
    self.myTableView.tableFooterView = [[UIView alloc] init];
    //
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addCommonRepair)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.curPage = 0;
    [self.dataSourceArr removeAllObjects];
    [self loadData];
}

- (void)loadData
{
    self.curPage++;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getPayServerAddr], commonRepairListURL];
    NSString *strPost;
    if(userID && userID.length>0)
    {
        strPost = [[NSString alloc] initWithFormat:@"userId=%@&page=%ld&rows=%ld", [AppDelegate getUserId], self.curPage, self.countPerPage];
    }
    else
    {
        strPost = [[NSString alloc] initWithFormat:@"page=%ld&rows=%ld", self.curPage, self.countPerPage];
    }
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self.dataSourceArr addObjectsFromArray:responseDic[@"rows"]];
            if(self.dataSourceArr.count < self.curPage*self.countPerPage)
            {
                [self.myTableView hasNoMoreData];
            }
            else
            {
                [self.myTableView hasMoreData];
            }
            [self.myTableView reloadData];
        }
        [self autoDecideToShowEmptyView];
    }];
}

/**
 添加公共报修
 */
- (void)addCommonRepair
{
    BIDAddCommonRepairViewController *vc = [[BIDAddCommonRepairViewController alloc] initWithNibName:@"BIDAddCommonRepairViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *identifier = @"identifier";
    NSUInteger row = indexPath.row;
    if(!_bRegister)
    {
        _bRegister = YES;
        UINib *nib = [UINib nibWithNibName:@"BIDCommonRepairCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDCommonRepairCell *repairCell = [tableView dequeueReusableCellWithIdentifier:identifier];
    NSDictionary *dic = self.dataSourceArr[row];
    repairCell.titleLabel.text = dic[@"content"];
    repairCell.timeLabel.text = [[NSString alloc] initWithFormat:@"报修时间：%@", dic[@"createTime"]];
    NSNumber *statusValue = dic[@"status"];
    NSString *strRepairStatus = @"";
    if([statusValue intValue] == 0)
    {
        strRepairStatus = @"待处理";
    }
    else
    {
        strRepairStatus = @"已处理";
    }
    repairCell.statusLabel.text = [[NSString alloc] initWithFormat:@"报修状态：%@", strRepairStatus];
    cell = repairCell;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.dataSourceArr[indexPath.row];
    BIDCommonRepairDetailViewController *vc = [[BIDCommonRepairDetailViewController alloc] initWithNibName:@"BIDCommonRepairDetailViewController" bundle:nil];
    vc.repairID = dic[@"id"];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 83.f;
}

@end
